﻿using MetroDataApi.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace MetroDataApi.Controllers.Management
{
    public class PrjStatusController : ApiController
    {

        // GET api/prjstatus/5
        public string Get(int prjId)
        {
            var list = PrjStatusHelper.GetByPrjId(prjId);
            return JsonConvert.SerializeObject(list);
        }

        // POST api/prjstatus
        public bool Post([FromBody]string value)
        {
            JObject jsonData = JObject.Parse(value);
            var item = jsonData.ToObject<ProjectStatusItem>();

            return PrjStatusHelper.SavePrjStatus(item);
        }
        // DELETE api/monitorpointsdictionary/5
        public bool Delete(int prjId, string date)
        {
            var jarr = JArray.Parse(date);

            var dateList = jarr.ToList();
            foreach (var d in dateList)
            {
                 PrjStatusHelper.DeletePrjStatus(prjId, d.ToString());
            }
            return true;
        }
    }


    /// <summary>
    /// 工况类的组合打包,方便数据传送
    /// </summary>
    public class ProjectStatusItem
    {
        public int PrjId { get; set; }
        public string Title { get; set; }
        public string Detail { get; set; }
        public List<PrjStatusImage> StatusImgs { get; set; }
        public string Date { get; set; }
        public ProjectStatusItem()
        {
            this.StatusImgs = new List<PrjStatusImage>();
        }
    }

    /// <summary>
    /// 工况类的帮助,方便前后端数据交换,储存
    /// </summary>
    public class PrjStatusHelper
    {
        public static List<ProjectStatusItem> GetByPrjId(int prjId)
        {
          

            var list = new List<ProjectStatusItem>();
            using (var db = new MetroDataDataContext())
            {
                
                var status = db.PrjStatus.Where(p => p.PrjId == prjId).OrderBy(p => p.Date);
                foreach (var s in status)
                {

                    var imgList = db.PrjStatusImage.Where(p => p.PrjId == prjId && p.Date == s.Date).ToList();
                    var item = new ProjectStatusItem()
                    {
                        Date = s.Date.Value.ToString("yyyy-MM-dd"),
                        Detail = s.Description,
                        PrjId = s.PrjId.Value,
                        StatusImgs = imgList,
                        Title = s.Status
                    };
                    list.Add(item);
                }
            }
            return list;
        }
        public static bool DeletePrjStatus(int prjId, string dateStr)
        {
            using (var db = new MetroDataDataContext())
            {
                try
                {
                    var date = DateTime.Parse(dateStr);
                    var prjSt = db.PrjStatus.Where(p => p.PrjId == prjId && p.Date.Value == date).ToList();
                    db.PrjStatus.DeleteAllOnSubmit(prjSt);//delete

                    var imgList = db.PrjStatusImage.Where(p => p.PrjId == prjId && p.Date.Value == date).ToList();
                    db.PrjStatusImage.DeleteAllOnSubmit(imgList);//delete

                    db.SubmitChanges();//submit
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public static bool SavePrjStatus(ProjectStatusItem item)
        {
            using (var db = new MetroDataDataContext())
            {
                try
                {
                    var date = DateTime.Parse(item.Date);
                    var bind = db.MetroProjectGroup.SingleOrDefault(p => p.PrjId == item.PrjId);
                    var bindList = new List<int>();
                    if (bind != null)
                    {
                        bindList = db.MetroProjectGroup.Where(p => p.GroupId == bind.GroupId).Select(p => p.PrjId.Value).ToList();
                    }
                    else {
                        bindList.Add(item.PrjId);
                    }
                    foreach (var id in bindList)
                    {
                        //先删除
                        PrjStatusHelper.DeletePrjStatus(id, item.Date);
                    }

                    foreach (var id in bindList)
                    {
     
                        var status = new PrjStatus()
                        {
                            Date = date,
                            Description = item.Detail,
                            PrjId = id,
                            Status = item.Title
                        };
                        db.PrjStatus.InsertOnSubmit(status);//insert

                        var imgs = item.StatusImgs.Select(t => new PrjStatusImage()
                        {
                            PrjId = id,
                            Url = t.Url,
                            Date = t.Date,
                            Description = t.Description
                        });
                        db.PrjStatusImage.InsertAllOnSubmit(imgs);//insert
                    }
                    db.SubmitChanges();//submit
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }


    }
}
